<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="活动编号" prop="no">
                            <el-input v-model="condition.search.no"
                                      placeholder="活动编号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="活动名称" prop="name">
                            <el-input v-model="condition.search.name"
                                      placeholder="活动名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="门店名称" prop="shopName">
                            <el-input v-model="condition.search.shopName"
                                      placeholder="门店名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="平台" prop="platform">
                            <el-select v-model="condition.search.platform"
                                       placeholder="平台">
                                <el-option label="小红书"
                                           value="XHS"></el-option>
                                <el-option label="微博"
                                           value="WB"></el-option>
                                <el-option label="点评"
                                           value="DP"></el-option>
                                <el-option label="抖音"
                                           value="DY"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="50">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="no"
                label="活动编号">
            </el-table-column>
            <el-table-column
                prop="name"
                label="活动名称">
            </el-table-column>
            <el-table-column
                prop="brandName"
                label="品牌名称">
            </el-table-column>
            <el-table-column
                prop="shopName"
                label="门店名称">
            </el-table-column>
            <el-table-column
                prop="platform"
                :formatter="formatPlatform"
                label="平台">
            </el-table-column>
            <el-table-column
                prop="unit"
                :formatter="formatUnit"
                label="招募单位">
            </el-table-column>
            <el-table-column
                prop="groupCount"
                label="每组多少人">
            </el-table-column>
            <el-table-column
                prop="total"
                label="总共人数">
            </el-table-column>
            <el-table-column
                prop="startDate"
                label="开始日期">
            </el-table-column>
            <el-table-column
                prop="endDate"
                label="结束日期">
            </el-table-column>
            <el-table-column
                prop="description"
                label="活动说明">
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="120">
                <template v-slot="scope">
                    <div>
                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click="onDetail(scope.row)" type="text" size="small">详情</el-button>
                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="活动编号">
                    <el-input v-model="addOrUpdateForm.no"
                              placeholder="活动编号"></el-input>
                </el-form-item>
                <el-form-item label="活动名称">
                    <el-input v-model="addOrUpdateForm.name"
                              placeholder="活动名称"></el-input>
                </el-form-item>
                <el-form-item label="品牌名称">
                    <el-input v-model="addOrUpdateForm.brandName"
                              placeholder="品牌名称"></el-input>
                </el-form-item>
                <el-form-item label="门店名称">
                    <el-input v-model="addOrUpdateForm.shopName"
                              placeholder="门店名称"></el-input>
                </el-form-item>
                <el-form-item label="平台">
                    <el-select v-model="addOrUpdateForm.platform"
                               placeholder="平台">
                        <el-option label="小红书"
                                   value="XHS"></el-option>
                        <el-option label="微博"
                                   value="WB"></el-option>
                        <el-option label="点评"
                                   value="DP"></el-option>
                        <el-option label="抖音"
                                   value="DY"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="招募单位">
                    <el-select v-model="addOrUpdateForm.unit"
                               placeholder="招募单位">
                        <el-option label="组"
                                   value="组"></el-option>
                        <el-option label="位"
                                   value="位"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="每组多少人">
                    <el-input v-model="addOrUpdateForm.groupCount"
                              placeholder="每组多少人"></el-input>
                </el-form-item>
                <el-form-item label="总共人数">
                    <el-input v-model="addOrUpdateForm.total"
                              placeholder="总共人数"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="开始日期">
                    <el-date-picker
                        v-model="addOrUpdateForm.startDate"
                        type="date"
                        placeholder="开始日期"
                        value-format="yyyy-MM-dd"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束日期">
                    <el-date-picker
                        v-model="addOrUpdateForm.endDate"
                        type="date"
                        placeholder="结束日期"
                        value-format="yyyy-MM-dd"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="活动说明">
                    <el-input v-model="addOrUpdateForm.description"
                              type="textarea"
                              placeholder="活动说明"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addTask, deleteTask, getTaskByPage, updateTask} from '@/api/api'

export default {
    name: "Task",
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {}
        }
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatPlatform: function (row, column, cellValue) {
            if (cellValue == 'XHS') {
                return "小红书";
            } else if (cellValue == 'WB') {
                return "微博";
            } else if (cellValue == 'DP') {
                return "点评";
            } else if (cellValue == 'DY') {
                return "抖音";
            } else {
                return "未知";
            }
        }, formatUnit: function (row, column, cellValue) {
            if (cellValue == '组') {
                return "组";
            } else if (cellValue == '位') {
                return "位";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addTask(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateTask(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDetail: function (row) {
            this.$router.push({path: '/k/task/detail', query: {id: row.id}});
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteTask({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            getTaskByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
